'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));
const fs = require('fs');


module.exports.userList = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 12;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('user').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('user').alias('user').join({
            table: 'usertype',
            join: 'left',
            as: 'type',
            on: ['type_id', 'type_id']
        }).where(where).order('user.user_id DESC').limit(start,limit).select().then(function(user_list){

            let data = {
                array:user_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"userList.html",data);
        });


    });

};


module.exports.userAdd = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('usertype').select().then(function(result){
        db.table('school').select().then(function(school){
            db.table('class').select().then(function(class_list){
                let Data = {
                    "type_list":result,
                    "school_list":school,
                    "class_list":class_list
                };

                CommonHelper.renderFile(req,res,"userAdd.html",Data);
            });
        });
        
    });
};

module.exports.userAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据

    //先检测一下 该用户是否存在
    let user_name = req.body.user_name;
    db.table('user').where(`user_name = '${user_name}'`).find().then(function(data){
        if(JSON.stringify(data) != "{}")
        {
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('该用户已被注册');location.href='/user/userAdd';</script>");
        }else{
            let data = {
                "user_name":req.body.user_name,
                "user_pwd":md5(req.body.user_pwd),
                "user_email":req.body.user_email,
                "user_time":Date.now(),
                "user_sex":req.body.user_sex,
                "user_address":req.body.user_address,
                "user_phone":req.body.user_phone,
                "user_action":'',
                "type_id":req.body.type_id
            };

            //是否有上传图片
            if(req.files.user_img.size > 0)
            {
                data.user_img = req.files.user_img.path.replace('assets','');
            }
            db.table('user').add(data).then(function(insertId){
                if(insertId)
                {
                    if(data.type_id == 2){
                        let teacher_data = {
                            "teach_name":req.body.teach_name,
                            "school_id":req.body.school_id,
                            "user_id":insertId,
                        };

                        db.table('teacher').add(teacher_data).then(function(teacherInsert){
                            if(teacherInsert){
                                //重定向
                                res.redirect(`/user/userList`);
                                return false;
                            }else{
                                //重定向
                                res.redirect(`/user/userAdd`);
                                return false;
                            }
                        });
                    } else if(data.type_id == 3){ //学生
                        let student_data = {
                            "stu_name":req.body.stu_name,
                            "stu_number":req.body.stu_number,
                            "class_id":req.body.class_id,
                            "school_id":req.body.school_id,
                            "user_id":insertId,
                        };

                        db.table('student').add(student_data).then(function(studentInsert){
                            if(studentInsert){
                                //重定向
                                res.redirect(`/user/userList`);
                                return false;
                            } else{
                                //重定向
                                res.redirect(`/user/userAdd`);
                                return false;
                            }
                        });
                    } else{ //超级管理员
                        //重定向
                        res.redirect(`/user/userList`);
                        return false;
                    }
                    
                }else{
                    //重定向
                    res.redirect(`/user/userAdd`);
                    return false;
                }
            });
        }
    });

};

module.exports.userEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let user_id = req.query.user_id;

    if(!user_id)
    {
        //重定向
        res.redirect(`/user/userList`);
        return false;
    }

    db.table('user').where(`user_id = ${user_id}`).find().then(function(user){
        // db.table('usertype').select().then(function(type_list){
            let Data = {
                "user":user,
                // "type_list":type_list
            };

            CommonHelper.renderFile(req,res,"userEdit.html",Data);
        // });
    });
};

module.exports.userEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let user_id = req.query.user_id;

    if(!user_id)
    {
        //重定向
        res.redirect(`/user/userList`);
        return false;
    }

    db.table('user').where(`user_id = ${user_id}`).find().then(function(user){

        let data = {
            "user_email":req.body.user_email,
            "user_sex":req.body.user_sex,
            "user_address":req.body.user_address,
            "user_phone":req.body.user_phone
        };

        //是否有密码修改
        if(req.body.user_pwd != "")
        {
            data.user_pwd = md5(req.body.user_pwd);
        }

        //是否有上传图片
        if(req.files.user_img.size > 0)
        {

            //同步获取文件的状态
            let user_old = fs.statSync(path.join("assets",user.user_img));

            user_old.isFile() && fs.unlinkSync(path.join("assets",user.user_img));

            data.user_img = req.files.user_img.path.replace('assets','');
        }

        db.table('user').where(`user_id = ${user_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/user/userList`);
                return false;
            }else{
                //重定向
                res.redirect(`/user/userEdit?user_id=${user_id}`);
                return false;
            }
        });

    });

};


module.exports.userAction = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let user_id = req.query.user_id;

    if(!user_id)
    {
        //重定向
        res.redirect(`/user/userList`);
        return false;
    }

    db.table('user').where(`user_id = ${user_id}`).find().then(function(user){
        db.table('user_action').select().then(function(result){

            //将用户权限转化为数组
            let user_action = user.user_action.split(",");

            let action_list = [];
            for(var item in result)
            {
                if(CommonHelper.inArray(user_action,result[item].action_name_en))
                {
                    result[item].checked = true;
                }else{
                    result[item].checked = false;
                }

                if(result[item].parent_id == 0)
                {
                    //顶级权限
                    action_list.push(result[item]);
                }

                //顶级权限
                for(var key in action_list)
                {

                    if(action_list[key].action_id == result[item].parent_id)
                    {
                        if(!action_list[key]['son'])
                        {
                            action_list[key]['son']=[];
                        }
                        action_list[key]['son'].push(result[item]);
                    }
                }
            }

            let Data = {
                "user":user,
                "action_list":action_list,
                "user_action":user_action,
            };

            CommonHelper.renderFile(req,res,"userAction.html",Data);
        });
    });
};


module.exports.userActionForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let user_id = req.query.user_id;
    let user_action = req.body.user_action || "";

    if(!user_id)
    {
        //重定向
        res.redirect(`/user/userList`);
        return false;
    }

    if(user_action)
    {
        //将数组转化为字符串
        let data = {
            "user_action":user_action.join(',')
        };

        db.table('user').where(`user_id = ${user_id}`).find().then(function(user){

            db.table('user').where(`user_id = ${user_id}`).update(data).then(function(affectId){
                if(affectId)
                {
                    if(user_id == req.session.user.user_id)
                    {
                        req.session.user.user_action = user_action.join(',');
                    }
                    res.redirect('/user/userList');
                    return false;
                }else{
                    res.redirect(`/user/userAction?user_id=${user_id}`);
                    return false;
                }
            })
        });

    } else if(user_action == ""){
        db.table('user').where(`user_id = ${user_id}`).find().then(function(user){
            let data = {
                "user_action":user_action
            };
            db.table('user').where(`user_id = ${user_id}`).update(data).then(function(affectId){
                if(affectId)
                {
                    if(user_id == req.session.user.user_id)
                    {
                        req.session.user.user_action = user_action;
                    }
                    res.redirect('/user/userList');
                    return false;
                }else{
                    res.redirect(`/user/userAction?user_id=${user_id}`);
                    return false;
                }
            })
        });
    }
};

module.exports.userDelete = function(req,res){

    let user_id = req.query.user_id;

    if(!user_id)
    {
        //重定向
        res.redirect(`/user/userList`);
        return false;
    }
    
    db.table('user').where(`user_id = ${user_id}`).find().then(function(user){
        if(user.type_id == 2){
            console.log("教师");
            db.table('teacher').where(`user_id = ${user.user_id}`).delete().then(function(affectId){
                if(affectId){
                    console.log("删除教师表成功！");
                }else{
                    console.log("删除教师表失败！");
                }
            });
            //重定向
            res.redirect(`/user/userList`);
            return false;
        }else if(user.type_id == 3){
            console.log("学生");
            db.table('student').where(`user_id = ${user.user_id}`).delete().then(function(affectId){
                if(affectId){
                    console.log("删除学生表成功！");
                }else{
                    console.log("删除学生表失败！");
                }
            });
            //重定向
            res.redirect(`/user/userList`);
            return false;
        }else{
            console.log("管理员");
        }

        res.redirect(`/user/userList`);
        return false;
    });



    db.table('user').where(`user_id = ${user_id}`).delete().then(function(affectId){
        if(affectId){
            console.log('删除账号表成功');
            //重定向
            res.redirect(`/user/userList`);
            return false;
        }else{
            console.log('删除账号表失败');
            //重定向
            res.redirect(`/user/userList`);
            return false;
        }
    });
   
};